package com.yaoweigroup.community.ui.bm;

import org.apache.http.entity.StringEntity;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.Shops;
import com.yaoweigroup.community.util.BitmapHelp;
import com.yaoweigroup.community.util.DialogUtil;
import com.yaoweigroup.community.util.JsonUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.UtilHttp;

/**
 * 详情页面
 * 
 * @author xi
 * 
 */
public class DetailActivity extends Activity implements OnClickListener {

	private RelativeLayout left_rl;
	private TextView shop_tv;
	private RelativeLayout comment_rl;

	private ImageView icon_iv;
	private TextView type_tv;
	private TextView name_tv;
	private TextView time_tv;
	private TextView phone_tv;
	private TextView address_tv;
	private RelativeLayout phone_rl;
	private RelativeLayout address_rl;
	private BitmapUtils bitmapUtils;
	private BitmapDisplayConfig bigPicDisplayConfig;
	private TextView des_tv;
	private TextView comment_tv;
	private Shops shop;
	private TextView center_tv;

	private ObjectMapper mapper;
	private Dialog dialog;
	private TextView discount_tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mapper = new ObjectMapper();
		dialog = DialogUtil.createWaitDialog(this, "数据获取中....");
		setContentView(R.layout.activity_detail);
		bitmapUtils = BitmapHelp.getBitmapUtils(this);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadFailedDrawable(this.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
		bigPicDisplayConfig.setLoadingDrawable(this.getResources().getDrawable(
				R.drawable.bucket_no_picture));
		center_tv = (TextView) findViewById(R.id.center_tv);
		comment_rl = (RelativeLayout) findViewById(R.id.comment_rl);
		comment_rl.setOnClickListener(this);
		left_rl = (RelativeLayout) findViewById(R.id.left_rl);
		left_rl.setOnClickListener(this);
		shop_tv = (TextView) findViewById(R.id.shop_tv);
		shop_tv.setOnClickListener(this);

		phone_tv = (TextView) findViewById(R.id.phone_tv);
		address_tv = (TextView) findViewById(R.id.address_tv);
		icon_iv = (ImageView) findViewById(R.id.icon_iv);
		type_tv = (TextView) findViewById(R.id.type_tv);
		name_tv = (TextView) findViewById(R.id.name_tv);
		time_tv = (TextView) findViewById(R.id.time_tv);
		phone_rl = (RelativeLayout) findViewById(R.id.phone_rl);
		phone_rl.setOnClickListener(this);
		address_rl = (RelativeLayout) findViewById(R.id.address_rl);
		address_rl.setOnClickListener(this);

		des_tv = (TextView) findViewById(R.id.des_tv);
		comment_tv = (TextView) findViewById(R.id.comment_tv);
		discount_tv = (TextView) findViewById(R.id.discount_tv);
		shop = (Shops) getIntent().getSerializableExtra("detail");
		if (shop != null) {
			shop_tv.setText("商品名字：" + shop.getShopName());
			name_tv.setText("商品名字：" + shop.getShopName());

			center_tv.setText(shop.getShopName());
			type_tv.setText("商品类别：" + shop.getTypeName());
			time_tv.setText("营业时间：" + shop.getWorktime() == null ? "暂无" : shop
					.getWorktime());
			String discountStr = shop.getPreferential();
			if (discountStr == null) {
				discountStr = "暂无优惠";
			}
			discount_tv.setText(Html.fromHtml(discountStr));
			String tel = shop.getTel();
			if (tel == null) {
				tel = "暂无";

			} else {
				if (tel.length() == 0)
					tel = "暂无";
			}
			phone_tv.setText("电话：   " + tel);
			address_tv.setText("地址：   " + shop.getAddress());
			String des = shop.getDes();
			if (des == null) {
				des = "暂无描述";

			} else {
				if (des.length() == 0)
					des = "暂无描述";
			}

			des_tv.setText(Html.fromHtml(des));
			bitmapUtils.display(icon_iv, Urls.imgUrl + shop.getImgPath(),
					bigPicDisplayConfig);
			// comment_tv.setText("共有" + shop.getCommentCounts() + "条评论");
		} else {
			String id = getIntent().getStringExtra("id");
			obtainDetail(id);
		}
	}

	private Intent intent;

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_rl:
			finish();
			break;
		case R.id.shop_tv:
			intent = new Intent(this, MenuInfoActivity.class);
			startActivity(intent);
			break;
		case R.id.comment_rl:
			intent = new Intent(this, CommentListActivity.class);
			intent.putExtra("id", shop.getId());
			startActivity(intent);
			break;
		case R.id.phone_rl:
			if (shop != null && shop.getTel() != null) {
				intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
						+ shop.getTel()));
				startActivity(intent);
			}

			break;
		case R.id.address_rl:
			intent = new Intent(this, RouteActivity.class);
			intent.putExtra("lat", shop.getLat());
			intent.putExtra("lng", shop.getLng());
			startActivity(intent);
			break;
		}
	}

	private void obtainDetail(String id) {
		dialog.show();
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("id", id);
		LogUtil.d("wxf", jsonUtil.toString());
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString());
		} catch (Exception e) {
			LogUtil.e("wxf", e.toString());
		}
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.getShop, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", "result = " + responseInfo.result);
						String result = responseInfo.result;
						try {
							JSONObject obj = new JSONObject(result);
							int state = obj.getInt("state");
							if (state == 200) {
								shop = mapper.readValue(obj.getString("data"),
										new TypeReference<Shops>() {
										});
								handler.sendEmptyMessage(DETAIL_SUCCESS);
							} else {
								handler.sendEmptyMessage(DETAIL_FAIL);
							}
						} catch (Exception e) {
							handler.sendEmptyMessage(DETAIL_FAIL);
							LogUtil.e("wxf", e.toString());
						}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
						handler.sendEmptyMessage(DETAIL_FAIL);

					}

				});
	}

	public final int DETAIL_SUCCESS = 0x2210;
	public final int DETAIL_FAIL = DETAIL_SUCCESS + 1;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			dialog.dismiss();
			switch (msg.what) {
			case DETAIL_SUCCESS:
				if (shop != null) {
					name_tv.setText("商品名字：" + shop.getShopName());
					shop_tv.setText("商品名字：" + shop.getShopName());
					center_tv.setText(shop.getShopName());
					type_tv.setText("商品类别：" + shop.getTypeName());
					time_tv.setText("营业时间：" + shop.getWorktime());
					String tel = shop.getTel();
					if (tel == null) {
						tel = "暂无";

					} else {
						if (tel.length() == 0)
							tel = "暂无";
					}
					phone_tv.setText("电话：   " + tel);
					address_tv.setText("地址：   " + shop.getAddress());
					String des = shop.getDes();
					if (des == null) {
						des = "暂无描述";

					} else {
						if (des.length() == 0)
							des = "暂无描述";
					}

					des_tv.setText(Html.fromHtml(des));
					bitmapUtils.display(icon_iv,
							Urls.imgUrl + shop.getImgPath(),
							bigPicDisplayConfig);
					// comment_tv.setText("共有" + shop.getCommentCounts() +
					// "条评论");
				}
				break;
			case DETAIL_FAIL:
				finish();
				Toast.makeText(DetailActivity.this, "数据获取失败", 500).show();
				break;
			}

		};
	};

}
